package com.lzh.shijicloudstack.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.List;

import static java.util.Collections.singletonList;

/**
 * 接口文档访问地址：http://localhost:9999/api/doc.html
 *
 * @author luzhiheng
 * @description swagger配置类
 */
//@Profile({"prod"})
@Profile({"dev", "loc", "dev"})
@EnableSwagger2
@Configuration
public class SwaggerConfig {

    @Bean
    public Docket getDocket() {
        //创建封⾯信息对象
        ApiInfoBuilder apiInfoBuilder = new ApiInfoBuilder();
        apiInfoBuilder.title("《lzh-template》后端接⼝说明")
                .description("此⽂档详细说明了 lzh-shijicloudstack 后端接⼝规范....")
                .version("v 1.0.0")
                .contact(new Contact("luzhiheng", "https://gitee.com/lu-zhihen/persevere-blog", "1203464373@qq.com"));
        ApiInfo apiInfo = apiInfoBuilder.build();

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo) //指定⽣成的⽂档中的封⾯信息：⽂档标题、版本、作者
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.lzh.shijicloudstack.controller"))
                .paths(PathSelectors.any())
                .build()
                .securityContexts(singletonList(securityContexts()))
                .securitySchemes(singletonList(securitySchemes()));
    }

    private SecurityScheme securitySchemes() {
        return new ApiKey("Authorization", "token", "header");
    }

    private SecurityContext securityContexts() {
        return SecurityContext.builder()
                .securityReferences(defaultAuth())
                .forPaths(PathSelectors.any())
                .build();
    }

    private List<SecurityReference> defaultAuth() {
        AuthorizationScope authorizationScope = new AuthorizationScope("xxx", "描述信息");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        return singletonList(new SecurityReference("Authorization", authorizationScopes));
    }
}